#if !SILVERLIGHT
using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Web.Services.Protocols;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml.Serialization;

namespace SBPweb.Common.Exceptions
{
	/// <summary>
	/// Class of ExceptionDescriptor
	/// </summary>
	[Serializable]
	[XmlType("ExceptionDescriptor")]
	public class ExceptionDescriptor : ISerializable
	{
		/// <summary>
		/// Protected message
		/// </summary>
		protected string mMessage;
		
		/// <summary>
		/// Message
		/// </summary>
		public string Message
		{
			get
			{
				return mMessage;
			}
			set
			{
				mMessage = value;
			}
		}

		/// <summary>
		/// Class of ExceptionDescriptor
		/// </summary>
		public ExceptionDescriptor()
		{
		}

		/// <summary>
		/// Class of ExceptionDescriptor
		/// </summary>
		/// <param name="message">message</param>
		public ExceptionDescriptor(string message)
		{
			mMessage = message;
		}

		/// <summary>
		/// Class of ExceptionDescriptor
		/// </summary>
		/// <param name="info">serialization info</param>
		/// <param name="context">streaming context</param>
		public ExceptionDescriptor(SerializationInfo info, StreamingContext context)
			: base()
		{
			mMessage = info.GetString("Message");
		}

		/// <summary>
		/// GetObjectData
		/// </summary>
		/// <param name="info">info</param>
		/// <param name="context">context</param>
		public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			info.AddValue("Message", mMessage);
		}

		/// <summary>
		/// Get a new SOAP exception
		/// </summary>
		/// <returns></returns>
		public virtual SoapException GetException()
		{
			string soapEnvelope = "";
			string soapMessage = mMessage;
			try
			{
				MemoryStream stream = new MemoryStream();
				SoapFormatter formatter =
					new SoapFormatter(null,
						new StreamingContext(
							StreamingContextStates.File));
				formatter.Serialize(stream, this);
				stream.Position = 0;
				StreamReader tr = new StreamReader(stream);
				soapEnvelope = tr.ReadToEnd();
			}
			catch (System.Exception ex)
			{
				soapMessage = ex.ToString() + soapMessage;
				soapEnvelope = "";
			}
			return new SoapException(soapMessage, SoapException.ClientFaultCode, soapEnvelope);
		}
	}
}
#endif